package com.zhilifang.modules.zlf.utils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.zhilifang.modules.zlf.entity.Sitemap;
/*
根据指定的规则，通过构造正则表达式获取网址
*/
public class Urls{
    private String startUrl;                                         //开始采集网址
    String  urlContent;
    String ContentArea;
    private String strAreaBegin ,strAreaEnd ;            //采集区域开始采集字符串和结束采集字符串
    String strContent;//获得的采集内容
    String[] allUrls;                                                            //采集到的所有网址
    private String  regex;                                                 //采集规则

    UrlAndTitle   urlAndTitle=new UrlAndTitle();    //存储网址和标题                    

    
    public static  List<Sitemap> runs(String locPrefix,String locSuffix){
    	Urls myurl=new Urls("<body","/body>");
        myurl.getStartUrl(locPrefix+locSuffix);
        myurl.getUrlContent();
        myurl.getContentArea();
        List<Sitemap>  list = myurl.Urls(locPrefix);
    	return list;
    }
    

    
    //初始化构造函数 strAreaBegin 和strAreaEnd

    public Urls (String strAreaBegin,String strAreaEnd){
        this.strAreaBegin=strAreaBegin;
        this.strAreaEnd=strAreaEnd;
    }
    static int id=1;
    public List<Sitemap> Urls(String loc){
    	 String regex ="<a.*?/a>";
         Pattern pt=Pattern.compile(regex);
         Matcher mt=pt.matcher(ContentArea);
         List<Sitemap> list = new ArrayList<Sitemap>();
         String val = "";
         while(mt.find()){
              //获取标题
              Matcher title=Pattern.compile(">.*?</a>").matcher(mt.group());
              Sitemap sitemap = new Sitemap();
              while(title.find()){
             	 val = title.group().replaceAll("[^\u4E00-\u9FA5]", "");		//去掉html标签只保留文字部分
             	 sitemap.setTitle(val);
              }
              //获取网址
              Matcher myurl=Pattern.compile("href=.*?>").matcher(mt.group()); 
              while(myurl.find()){
             	 val = myurl.group().replaceAll("href=|>","") .replaceAll("style=\"(.*?)\"","").replaceAll("target=\"(.*?)\"","")
             			 .replaceAll("rel=\"(.*?)\"","") .replaceAll("title=\"(.*?)\"","") .replaceAll("id=\"(.*?)\"","") .replaceAll("\"","");
             	 if(val.indexOf("http:")==-1){
             		 val = loc+val;
             	 }
             	 sitemap.setLoc(val);
              }
              sitemap.setId(id);
              sitemap.setLastmod(Utils.dataStringSub(Utils.createThisTime()));
              sitemap.setChangefreq("monthly");
              sitemap.setPriority("0.8");
              list.add(sitemap);
             id++;
             val = "";
         }
		return list;

    }    

    //获得开始采集网址
    public void getStartUrl(String startUrl)
    {
        this.startUrl=startUrl;
    }

    //获得网址所在内容;
    public void getUrlContent()
    {

        StringBuffer is=new StringBuffer();
        try
        {
            URL myUrl=new URL(startUrl);
            BufferedReader br= new BufferedReader(
                                                        new InputStreamReader(myUrl.openStream()));

            String s;                                                
            while((s=br.readLine())!=null)
            {
                is.append(s);
            }                                            
            urlContent=is.toString();
        }
    catch(Exception e)

    { 
        System.out.println("网址文件未能输出");
        e.printStackTrace();
    }

        
    }

    
    //获得网址所在的匹配区域部分
    public void getContentArea()
    {
         int pos1=0,pos2=0;
         pos1= urlContent.indexOf(strAreaBegin)+strAreaBegin.length();
         pos2=urlContent.indexOf(strAreaEnd,pos1);
         ContentArea=urlContent.substring(pos1,pos2); 
    }


    //获取采集规则

    //获取url网址
    public void getUrl()
    {

    }

    public String getRegex()
    {
        return regex;

    }

    class UrlAndTitle
    {
        String myURL;
        String title;
    }
}
